package com.aoxon.cargo.component;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.aoxon.cargo.R;

public abstract class WaterFall extends ScrollView {

	
	private LinearLayout llContent ,llRoot, svChild;
	private View headView, footView;

	private Context context;
	private LayoutInflater inflater;

	private int columnCount = 0;
	private int oldChildCount = 0;
	private LinearLayout[] columnArr;
	private long[] heightArr;
	// viewMap 用于记录所有llContet里的position和对应的View
	// private Map<Integer, View> viewMap = new HashMap<Integer, View>();
	private Map<Integer, Integer>[] maHeight;
	private Map<Integer, View>[] maView;

	public WaterFall(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		setup( context );
	}

	public WaterFall(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		setup( context );
	}

	private void setup(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		svChild = (LinearLayout) inflater.inflate(
				R.layout.water_fall_content, null);
		this.llContent = (LinearLayout) svChild.findViewById(R.id.ll_content);
		this.addView(svChild);
		
	}

	public void notifyDataSetChanged() {
		int newChildCount = getContentChildCount();
		//从最旧的下一个position开始,添加,这样就要保证旧的是没有改变的
		for (int i = oldChildCount; i < newChildCount; i++) {
			View child = getView(i);
			addColumnView(child, getHeight(i));
		}
	}

	public void removeAllViews() {
		// llContent.removeAllViews();
		for (int i = 0; i < columnCount; i++) {
			columnArr[i].removeAllViews();
		}
		oldChildCount = 0;
	}

	public final void deleteChildAt(int position) {
		
//		if (viewMap.containsKey(position)) {
//			// llContent.removeViewAt(position);
//			View child = viewMap.get(position);

			for (int i = 0; i < columnCount; i++) {
				if(maView[i].containsKey(position)){
					columnArr[i].removeView(maView[i].get(position));
					heightArr[i] -= maHeight[i].get(position);
					deleteViewMa(i, position);
					break;
				}
			}
			oldChildCount -= 1;

	}

	public int getHeight(int position) {
		return 1;
	}

	private void addColumnView(View view, int height) {
		int minHeight = 0;
		for (int i = 1; i < columnCount; i++) {
			if (heightArr[i] < heightArr[minHeight]) {
				minHeight = i;
			}
		}
		columnArr[minHeight].addView(view);
		oldChildCount++;
		heightArr[minHeight] += height;
	}

	private void addViewMa(int column, int position, int height, View child) {
		maHeight[column].put(position, height);
		maView[column].put(position, child);
	}

	private void deleteViewMa(int column, int position) {
		maHeight[column].remove(position);
		maView[column].remove(position);
	}

	public final void init(int columnCount) {
		if (columnCount > 0) {
			columnArr = new LinearLayout[columnCount];
			heightArr = new long[columnCount];
			maHeight = new HashMap[columnCount];
			maView = new HashMap[columnCount];
			this.columnCount = columnCount;
			for (int i = 0; i < columnCount; i++) {
				LinearLayout llColumn = new LinearLayout(context);
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT,
						(float) 1.0);
//				llColumn.setLayoutParams(params);
				llColumn.setOrientation(LinearLayout.VERTICAL);
				llContent.addView(llColumn, params);
				columnArr[i] = llColumn;
				heightArr[i] = 0;
				maHeight[i] = new HashMap<Integer, Integer>();
				maView[i] = new HashMap<Integer, View>();
			}
		}
	}

	//返回列数
	public int getColumnCount(){
		return columnCount;
	}
	
	//在顶部添加一个View
	public final void addHeadView(View view){
		svChild.addView(view, 0);
	}
	
	//在底部添加一个View
	public final void addFootView(View view){
		svChild.addView(view);
	}
	
	public final void addViewInColumn(int column , View view , int height){
		if(column < columnCount){
			columnArr[column].addView(view);
			heightArr[column] += height;
		}
	}
	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub
		// 滑到底部
		if (svChild.getMeasuredHeight() <= getScrollY() + getHeight()) {
			scrollToBottom();
		}
		super.onScrollChanged(l, t, oldl, oldt);
	}

	public abstract void scrollToBottom();

	//notifyDataSetChange时调用来获取子View
	//该方法实现,用于返回子View
	public abstract View getView(int position);

	public abstract int getContentChildCount();
}
